<?php
/*

    ZendoFramework Tarball Explorer
        < http://zendoframework.org/zendo.php.tar >

    Copyright 2008 - The ZendoFramework Development Team
        < http://zendoframework.org/team >

    ZendoFramework is released under the
    GNU General Public License (GPL) version 3
    provided with this release in license.txt
    or via web at < http://gnu.org/licenses/gpl.txt >

*/

//
// Defines constants that represent the diffrents files types
//
define('Zendo_Tar_File', 0);
define('Zendo_Tar_Dir', 5);

//
// Class Zendo_Tar
//
class Zendo_Tar 
{
	var $filePath = false;
    var $fileRead = false;
    
    var $tarSource = '';
    var $tarContents = array();
    
    function Zendo_Tar($path)
    {
    	$this->filePath = $path;
        
        $this->fileRead = @fopen($this->filePath, 'rb');
        
        if(!$this->fileRead)
            return false;
            
        while(!feof($this->fileRead))
        	$this->tarSource .= fread($this->fileRead, 8192);
        fseek($this->fileRead, 0);
        
        $this->_import();
        
        return true;
    }
    
    function _import()
    {
    	$results = array();
        
    	$source = $this->tarSource;
    	$length = strlen($source);
        $offset = 0;
        
        while($offset < $length)
        {
        	$filename = rtrim(substr($source, $offset, 100), "\0");
            $filesize = substr($source, $offset+124, 12);
            $filetype = substr($source, $offset+156, 1);
            
            $offset += 512;
            
            preg_match('/([0-7]+)/', $filesize, $preg);
            $contents = substr($source, $offset, octdec($preg[1]));
            
            $offset += octdec($preg[1]);
            
            while($offset % 512 != 0)
                $offset++;
                
            if(($filetype != Zendo_Tar_File && $filetype != Zendo_Tar_Dir) || $filetype == "\0")
                continue;
                
            $results[$filename] = array($filetype, $contents);
        }
        
        $this->tarContents = $results;
    }
    
    function get($path)
    {
    	return isset($this->tarContents[$path]) ? $this->tarContents[$path][1] : false;
    }
    
    function listContent()
    {
        $list = array();
        
        foreach($this->tarContents as $filename => $null)
        {
        	$list[] = $filename;
        }
        
        return ($list == array()) ? false : $list;
    }
    
    function file_exists($path)
    {
        return isset($this->tarContents[$path]);
    }
    
    function is_file($path)
    {
        return isset($this->tarContents[$path]) ? ($this->tarContents[$path][0] == Zendo_Tar_File) : false;
    }
    
    function is_dir($path)
    {
        return isset($this->tarContents[$path]) ? ($this->tarContents[$path][0] == Zendo_Tar_Dir) : false;
    }
}

//
// Function-oriented style
//
$LAST_ZENDO_TAR_OPENED = null;

function &zendo_tar_open($path)
{
	global $LAST_ZENDO_TAR_OPENED;
    
    $tar = new Zendo_Tar($path);
    $LAST_ZENDO_TAR_OPENED = $tar;
    
    return ($tar->fileRead) ? $tar : false;
}

function zendo_tar_get($param1, $param2 = false)
{
	if(!$param2)
    {
    	$param2 = $param1;
        
        global $LAST_ZENDO_TAR_OPENED;
        $param1 = $LAST_ZENDO_TAR_OPENED;
    }
    
    if(!is_object($param1) || !$param1->fileRead)
        return false;
    
    return $param1->get($param2);
}

function zendo_tar_listContent($param1 = false)
{
    if(!$param1)
    {
        global $LAST_ZENDO_TAR_OPENED;
        $param1 = $LAST_ZENDO_TAR_OPENED;
    }
    
    if(!is_object($param1) || !$param1->fileRead)
        return false;
    
    return $param1->listContent();
}

function zendo_tar_file_exists($param1, $param2 = false)
{
    if(!$param2)
    {
        $param2 = $param1;
        
        global $LAST_ZENDO_TAR_OPENED;
        $param1 = $LAST_ZENDO_TAR_OPENED;
    }
    
    if(!is_object($param1) || !$param1->fileRead)
        return false;
    
    return $param1->file_exists($param2);
}

function zendo_tar_is_file($param1, $param2 = false)
{
    if(!$param2)
    {
        $param2 = $param1;
        
        global $LAST_ZENDO_TAR_OPENED;
        $param1 = $LAST_ZENDO_TAR_OPENED;
    }
    
    if(!is_object($param1) || !$param1->fileRead)
        return false;
    
    return $param1->is_file($param2);
}

function zendo_tar_is_dir($param1, $param2 = false)
{
    if(!$param2)
    {
        $param2 = $param1;
        
        global $LAST_ZENDO_TAR_OPENED;
        $param1 = $LAST_ZENDO_TAR_OPENED;
    }
    
    if(!is_object($param1) || !$param1->fileRead)
        return false;
    
    return $param1->is_dir($param2);
}

